use axum::extract::ws::{CloseFrame as AxumCloseFrame, Message as AxumMessage};
use tokio_tungstenite::tungstenite::{
    protocol::CloseFrame as TuneCloseFrame, Message as TungMessage,
};

pub fn map_from_axum_ws_message(msg: AxumMessage) -> TungMessage {
    match msg {
        AxumMessage::Binary(binary) => TungMessage::Binary(binary),
        AxumMessage::Ping(ping) => TungMessage::Ping(ping),
        AxumMessage::Pong(pong) => TungMessage::Pong(pong),
        AxumMessage::Text(text) => TungMessage::Text(text),
        AxumMessage::Close(close) => TungMessage::Close(close.map(|v| TuneCloseFrame {
            code: v.code.into(),
            reason: v.reason,
        })),
    }
}

pub fn map_to_axum_ws_message(msg: TungMessage) -> AxumMessage {
    match msg {
        TungMessage::Binary(binary) => AxumMessage::Binary(binary),
        TungMessage::Ping(ping) => AxumMessage::Ping(ping),
        TungMessage::Pong(pong) => AxumMessage::Pong(pong),
        TungMessage::Text(text) => AxumMessage::Text(text),
        TungMessage::Close(close) => AxumMessage::Close(close.map(|v| AxumCloseFrame {
            code: v.code.into(),
            reason: v.reason,
        })),
        msg => panic!("unsupported message: {msg:?}"),
    }
}
